package ipt.capitulo19;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.net.URL;
public class BarraBotones2 extends JToolBar{
    JButton boton1;
    JTextField urlTexto;
    ExploradorHTML apl;
    Border bordePulsado, bordeNormal; 
    BarraBotones2(ExploradorHTML apl){
        this.apl=apl;
        bordeNormal = new SoftBevelBorder(BevelBorder.RAISED);  
        bordePulsado = new SoftBevelBorder(BevelBorder.LOWERED);
        boton1 = new JButton();
        boton1.setIcon(new ImageIcon("Abrir.gif"));
        boton1.setToolTipText("Abrir");
        boton1.setName("Abrir");
        boton1.setToolTipText("Abre un fichero existente");
        boton1.setBorder(bordeNormal);
        boton1.setMnemonic(KeyEvent.VK_I);
        boton1.addMouseListener(new VigilaBarraHerr());
        setLayout(new FlowLayout(FlowLayout.LEFT));
        urlTexto = new JTextField(" ");
        urlTexto.setPreferredSize(new Dimension(400,25));
        urlTexto.addActionListener(new VigilaCasillaTexto());
        add(urlTexto);
        add(boton1);
        setFloatable(false);
    } 
    class VigilaBarraHerr extends MouseAdapter{
        public void mousePressed(MouseEvent e){
           Component aux = e.getComponent();
        ((JButton)aux).setBorder(bordePulsado);  
    }
    public void mouseReleased(MouseEvent e){
       Component aux = e.getComponent();
       ((JButton)aux).setBorder(bordeNormal); 
       Frame ventana = new Frame(); 
       if(aux.getName().equals("Abrir")){
          FileDialog dialogo= new FileDialog(ventana,"Selecciona un fichero de texto");                                        
          dialogo.setDirectory("c:\\");
          dialogo.setMode(FileDialog.LOAD);
          dialogo.setVisible(true);
          String nombreDirectorio = (String)dialogo.getDirectory();
          String nombreFichero = (String)dialogo.getFile();
          try {
             if((nombreFichero!=null) && (nombreDirectorio !=null)){
               URL fichero = new URL("file:"+nombreDirectorio+nombreFichero);
               apl.jeditorpane.setPage(fichero);
               urlTexto.setText(fichero.toString());
               apl.htmlTexto.setText(null);
               apl.lF.leeFicheroURL(fichero,apl.htmlTexto);
               apl.carpeta.repaint();
             }   
          }catch(Exception ex) {
             ex.printStackTrace();
          }
       }
    }
  }
  class VigilaCasillaTexto implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        try{
            apl.jeditorpane.setPage(urlTexto.getText());
            apl.htmlTexto.setText(null);
            apl.lF.leeFicheroURL(new URL(urlTexto.getText()),apl.htmlTexto);
            apl.carpeta.repaint();
        }catch(Exception ex){
            System.out.println("error URL ");
        }
        
    }
} 
}
